﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;

namespace Logistics
{
    public partial class frmUsers : Form
    {
        ListViewColumnSorter lvwColumnSorter = new ListViewColumnSorter();
        Manager.tc_Department objDepartment = new Manager.tc_Department();
        Manager.tc_Permission objPermissions = new Manager.tc_Permission();
        List<Model.tc_Department> dList = null;
        Manager.tc_Role objRole = new Manager.tc_Role();
        List<Model.tc_Role> rList = null;
        public frmUsers()
        {
            InitializeComponent();
            this.listInfo.ListViewItemSorter = lvwColumnSorter;
            ImageList imgList = new ImageList();
            imgList.ImageSize = new Size(1, 20);//分别是宽和高
            listInfo.SmallImageList = imgList;
            dList = objDepartment.GetList();
            rList = objRole.GetList();
        }

        #region  编辑
        public void Find()
        {

        }
        public void Add()
        {
            var frm = new frmUsersModify();
            frm.OnModelEvent = AddListviewItem;
            frm.ShowDialog();
        }
        public void Modify()
        {
            if (this.listInfo.SelectedItems.Count == 0)
                return;
            var id = Guid.Parse(this.listInfo.SelectedItems[0].Tag.ToString());
            var objUser = new Manager.tc_User();
            var model = objUser.GetModel(id);
            model.Department = dList.FirstOrDefault(m => m.Id == model.DepartmentId)?.Department;
            model.Role = rList.FirstOrDefault(m => m.Id == model.RoleId)?.Role;
            var frm = new frmUsersModify(model);
            frm.OnModelEvent = AddListviewItem;
            frm.ShowDialog();
        }
        public void Delete()
        {
            if (this.listInfo.SelectedItems.Count == 0)
                return;
            var item = this.listInfo.SelectedItems[0];
            var msg = "确定要删除'" + item.Text + "'吗？";
            if (MessageBox.Show(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                var id = Guid.Parse(item.Tag.ToString());
                var objRole = new Manager.tc_User();
                objRole.Delete(id);
                DataBind();
            }

        }

        public void Refreshs()
        {
            DataBind();
        }
        #endregion

        private void AddListviewItem(Model.tc_User model)
        {
            ListViewItem item = null;
            if (this.listInfo.Items.Count > 0)
            {
                item = MyForms.FindItemWithTag(listInfo, model.Id.ToString());
            }
            if (item == null)
            {
                item = new ListViewItem();
                item.Tag = model.Id.ToString();
                item.Text = model.UserName?.Trim();
                item.SubItems.Add(model.Name?.Trim());
                item.SubItems.Add(model.Department?.Trim());
                item.SubItems.Add(model.Role?.Trim());
                item.SubItems.Add("******");
                item.SubItems.Add(model.IsActive == true ? "有效" : "无效");
                item.SubItems.Add(model.ButtonPermissions?.Trim());
                item.SubItems.Add(model.LogisticsPermissions?.Trim());
                this.listInfo.Items.Add(item);
            }
            else
            {
                item.Text = model.UserName.Trim();
                item.SubItems[1].Text = model.Name.Trim();
                item.SubItems[2].Text = model.Department?.Trim();
                item.SubItems[3].Text = model.Role?.Trim();
                item.SubItems[4].Text = "******";
                item.SubItems[5].Text = model.IsActive == true ? "有效" : "无效";
                item.SubItems[6].Text = model.ButtonPermissions?.Trim();
                item.SubItems[7].Text = model.LogisticsPermissions?.Trim();
                if (model.Id != Guid.Empty)
                {
                    if (Guid.Parse(item.Tag.ToString()) == model.Id)
                    {
                        item.Selected = true;
                        item.Focused = true;
                        this.listInfo.EnsureVisible(this.listInfo.Items.IndexOf(item)); // 使该项可见
                        this.listInfo.Focus();
                    }
                }
            }
        }
        public void DataBind()
        {
            this.listInfo.Items.Clear();
            this.listInfo.BeginUpdate();
            var objUser = new Manager.tc_User();
            var list = objUser.GetList();
            var objTC_OWI_FILE = new Manager.TC_OWI_FILE();
            var fileLists = objTC_OWI_FILE.GetList();
            foreach (var item in list)
            {
                item.Department = dList.FirstOrDefault(m => m.Id == item.DepartmentId)?.Department;
                item.Role = rList.FirstOrDefault(m => m.Id == item.RoleId)?.Role;
                var plist = objPermissions.GetList(item.Id, Model.EPermissionType.Buttons).Where(m => m.IsGranted);
                var permissions = new List<string>();
                foreach (var p in plist)
                {
                    if (p.Name == ButtonPermissions.SAPInport.ToString())
                        permissions.Add("SAP导入");
                    if (p.Name == ButtonPermissions.DataView.ToString())
                        permissions.Add("数据查看");
                    if (p.Name == ButtonPermissions.Export.ToString())
                        permissions.Add("数据导出");
                    if (p.Name == ButtonPermissions.PermissionsAdministartor.ToString())
                        permissions.Add("权限管理");
                }
                item.ButtonPermissions = string.Join("/", permissions.ToArray());
                plist = objPermissions.GetList(item.Id, Model.EPermissionType.Logistics).Where(m => m.IsGranted);

                permissions = (from p in plist
                               join f in fileLists on p.Name equals f.TC_OWI001
                               select f.TC_OWI002).ToList();

                item.LogisticsPermissions = string.Join("/", permissions.ToArray());
                AddListviewItem(item);
            }
            this.listInfo.EndUpdate();
        }
        private void listInfo_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            if (e.Column == lvwColumnSorter.SortColumn)
            {
                if (lvwColumnSorter.Order == SortOrder.Ascending)
                {
                    lvwColumnSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvwColumnSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                lvwColumnSorter.SortColumn = e.Column;
                lvwColumnSorter.Order = SortOrder.Ascending;
            }
            this.listInfo.Sort();
        }

        private void listInfo_DoubleClick(object sender, EventArgs e)
        {
            Modify();
            DataBind();
        }

        private void frmUsers_Load(object sender, EventArgs e)
        {
            DataBind();
        }

        private void AddMenuItem_Click(object sender, EventArgs e)
        {
            Add();
            DataBind();
        }

        private void ModifyMenuItem_Click(object sender, EventArgs e)
        {
            Modify();
            DataBind();
        }

        private void DelMenuItem_Click(object sender, EventArgs e)
        {
            Delete();
            DataBind();
        }

        private void RefreshMenuItem_Click(object sender, EventArgs e)
        {
            Refreshs();
            DataBind();
        }

        private void butAdd_Click(object sender, EventArgs e)
        {
            Add();
            DataBind();
        }

        private void butModify_Click(object sender, EventArgs e)
        {
            Modify();
            DataBind();
        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            Delete();
            DataBind();
        }

        private void butIsActive_Click(object sender, EventArgs e)
        {
            if (this.listInfo.SelectedItems.Count == 0)
                return;
            var id = Guid.Parse(this.listInfo.SelectedItems[0].Tag.ToString());
            var objUser = new Manager.tc_User();
            var model = objUser.GetModel(id);
            model.Department = dList.FirstOrDefault(m => m.Id == model.DepartmentId)?.Department;
            model.Role = rList.FirstOrDefault(m => m.Id == model.RoleId)?.Role;
            var frm = new frmUserActive(model);
            frm.OnModelEvent = AddListviewItem;
            frm.ShowDialog();
            DataBind();
        }

        private void butReset_Click(object sender, EventArgs e)
        {
            if (this.listInfo.SelectedItems.Count == 0)
                return;
            var id = Guid.Parse(this.listInfo.SelectedItems[0].Tag.ToString());
            var objUser = new Manager.tc_User();
            var model = objUser.GetModel(id);
            model.Department = dList.FirstOrDefault(m => m.Id == model.DepartmentId)?.Department;
            model.Role = rList.FirstOrDefault(m => m.Id == model.RoleId)?.Role;
            var frm = new frmUserResetPassword(model);
            frm.OnModelEvent = AddListviewItem;
            frm.ShowDialog();
            DataBind();
        }

        private void butPermission_Click(object sender, EventArgs e)
        {
            if (this.listInfo.SelectedItems.Count == 0)
                return;
            var id = Guid.Parse(this.listInfo.SelectedItems[0].Tag.ToString());
            var objUser = new Manager.tc_User();
            var model = objUser.GetModel(id);
            model.Department = dList.FirstOrDefault(m => m.Id == model.DepartmentId)?.Department;
            model.Role = rList.FirstOrDefault(m => m.Id == model.RoleId)?.Role;
            var frm = new frmUserButtonPermissions(model);
            frm.OnModelEvent = AddListviewItem;
            frm.ShowDialog();
            DataBind();
        }

        private void butLogisticsPermission_Click(object sender, EventArgs e)
        {
            if (this.listInfo.SelectedItems.Count == 0)
                return;
            var id = Guid.Parse(this.listInfo.SelectedItems[0].Tag.ToString());
            var objUser = new Manager.tc_User();
            var model = objUser.GetModel(id);
            model.Department = dList.FirstOrDefault(m => m.Id == model.DepartmentId)?.Department;
            model.Role = rList.FirstOrDefault(m => m.Id == model.RoleId)?.Role;
            var frm = new frmUserLogisticsPermissions(model);
            frm.OnModelEvent = AddListviewItem;
            frm.ShowDialog();
            DataBind();
        }

        private void groupPanel2_Click(object sender, EventArgs e)
        {

        }
    }
}
